package com.ruihui.activity.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期相关的操作
 * @author Dawei
 *  
 */

public class DateUtil {

	/**
	 * 将一个字符串转换成日期格式
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date toDate(String date, String pattern) {
		if((""+date).equals("")){
			return null;
		}
		if(pattern == null){
			pattern = "yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date newDate = new Date();
		try {
			newDate = sdf.parse(date);
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return newDate;
	}
	
	/**
	 * 把日期转换成字符串型
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String toString(Date date, String pattern){
		if(date == null){
			return "";
		}
		if(pattern == null){
			pattern = "yyyy-MM-dd";
		}
		String dateString = "";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			dateString = sdf.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return dateString;
	}
	
	public static String toString(Long time,String pattern){
		if(time>0){
			if(time.toString().length()==10){
				time = time*1000;
			}
			Date date = new Date(time);
			String str  = DateUtil.toString(date, pattern);
			return str;
		}
		return "";
	}

	
	
	/**
	 * 获取上个月的开始结束时间
	 * @return
	 */
	public static Long[] getLastMonth() {
		   // 取得系统当前时间
		   Calendar cal = Calendar.getInstance();
		   int year = cal.get(Calendar.YEAR);
		   int month = cal.get(Calendar.MONTH) + 1;
		   
		   // 取得系统当前时间所在月第一天时间对象
		   cal.set(Calendar.DAY_OF_MONTH, 1);
		   
		   // 日期减一,取得上月最后一天时间对象
		   cal.add(Calendar.DAY_OF_MONTH, -1);
		   
		   // 输出上月最后一天日期
		   int day = cal.get(Calendar.DAY_OF_MONTH);

		   String months = "";
		   String days = "";

		   if (month > 1) {
		    month--;
		   } else {
		    year--;
		    month = 12;
		   }
		   if (!(String.valueOf(month).length() > 1)) {
		    months = "0" + month;
		   } else {
		    months = String.valueOf(month);
		   }
		   if (!(String.valueOf(day).length() > 1)) {
		    days = "0" + day;
		   } else {
		    days = String.valueOf(day);
		   }
		   String firstDay = "" + year + "-" + months + "-01";
		   String lastDay = "" + year + "-" + months + "-" + days;

		   Long[] lastMonth = new Long[2];
		   lastMonth[0] =DateUtil.getDateline(firstDay);
		   lastMonth[1] = DateUtil.getDateline(lastDay);

		 //  //System.out.println(lastMonth[0] + "||" + lastMonth[1]);
		   return lastMonth;
		}
	
	
	/**
	 * 获取当月的开始结束时间
	 * @return
	 */
	public static Long[] getCurrentMonth() {
		   // 取得系统当前时间
		   Calendar cal = Calendar.getInstance();
		   int year = cal.get(Calendar.YEAR);
		   int month = cal.get(Calendar.MONTH)+1 ;
		   // 输出下月第一天日期
		   int notMonth = cal.get(Calendar.MONTH)+2 ;
		   // 取得系统当前时间所在月第一天时间对象
		   cal.set(Calendar.DAY_OF_MONTH, 1);
		   
		   // 日期减一,取得上月最后一天时间对象
		   cal.add(Calendar.DAY_OF_MONTH, -1);
		   
		  

		   String months = "";
		   String nextMonths = "";


		   if (!(String.valueOf(month).length() > 1)) {
		    months = "0" + month;
		   } else {
		    months = String.valueOf(month);
		   }
		   if (!(String.valueOf(notMonth).length() > 1)) {
			   nextMonths = "0" + notMonth;
		   } else {
			   nextMonths = String.valueOf(notMonth);
		   }
		   String firstDay = "" + year + "-" + months + "-01";
		   String lastDay=	""+year+"-"+nextMonths+"-01";
		   Long[] currentMonth = new Long[2]; 
		   currentMonth[0] =DateUtil.getDateline(firstDay);
		   currentMonth[1] = DateUtil.getDateline(lastDay);

		 //  //System.out.println(lastMonth[0] + "||" + lastMonth[1]);
		   return currentMonth;
		}
	//获取当前时间的第前N天或后N天(number为负就是前几天，number为正就是后几天)
	public static Date GetThisTimeToNumber(int number){
		Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, number);
        Date date = cal.getTime();
        return date;
	}
	//获取指定时间的第前N天或后N天(number为负就是前几天，number为正就是后几天)
	public static Date GetDateTimeToNumber(Date date,int number){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
        cal.add(Calendar.DATE, number);
        Date dates = cal.getTime();
        return dates;
	}
	
	//获取指定时间的毫秒数
	public static long getTheCurrentMillionSeconds(Date date) {
		long currentTime = date.getTime();
		return currentTime;
	}	
 
	public static long getDateline(){
		return System.currentTimeMillis()/1000;
	}
	//获取当天0点时间戳
	public static long getDateStartLine(){
		long current=System.currentTimeMillis();//当前时间毫秒数
		long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
		return zero/1000;
	}
	//获取当天12点时间戳
	public static long getDateEndLine(){
		long current=System.currentTimeMillis();//当前时间毫秒数
		long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
		long twelve=zero+24*60*60*1000-1;//今天23点59分59秒的毫秒数
		return twelve/1000;
	}
	public static long getDateline(String date){
		return (long)(toDate(date, "yyyy-MM-dd").getTime()/1000);
	}
	public static long getDateHaveHour(String date){
		return (long)(toDate(date, "yyyy-MM-dd HH").getTime()/1000);
	}
	public static long getDateline(String date,String pattern){
		return (long)(toDate(date, pattern).getTime()/1000);
	}
	
	/*
	 * 两个时间戳之间小时(秒)
	 */
	public static double getBetweenHourTime(long minTime, long maxTime) {
		double hour = 0.00;
		double diff = 0.00;
	    diff =  maxTime - minTime;
        hour = (diff / (60 * 60 ));
		return hour;
	}
	
	public static void main(String[] args){
		
		/*	long d= 1319990400 ;
					d=d*1000;
			int line =getDateline("2011-10-31");
		
			//System.out.println( line +   "--"+toString(new Date(d), "yyyy-MM-dd"));
			//System.out.println(d);*/
		
		//2017/3/6/09：36：12’ 
		//2017/3/15/10：38：45’ 
		//2017/3/21/16：12：34’ 
		//2017/3/38/15：52：45’

		
		//long d1 =getDateline("2017-03-21 16:12");
		//long d2 =getDateline("2017-03-28 15:52");
//		
		//System.out.println(d1);
		//System.out.println(d2);
		
//		System.out.println(getDateline("2017-03-28 09:46:03"));
//		System.out.println(getDateline("2017-04-01 17:16:47"));
//		System.out.println(getDateline("2017-04-01 20:48:11"));
//		System.out.println(getDateline("2017-04-24 19:37:07"));
//		System.out.println(getDateline("2017-04-27 10:23:16"));
//		System.out.println(getDateline("2017-03-27 21:13:47"));
//		System.out.println(getDateline("2017-04-19 18:43:19"));
//		System.out.println(getDateline("2017-05-01 18:13:25"));
		
//		System.out.println(getDateline("2017-05-05 22:48:36"));
//		System.out.println(getDateline("2017-04-25 13:01:45"));
//		System.out.println(getDateline("2017-05-03 16:17:52"));
//		System.out.println(getDateline("2017-05-13 16:12:36"));
//		System.out.println(getDateline("2017-05-13 16:26:45"));
//		System.out.println(getDateline("2017-04-26 17:48:36"));
//		System.out.println(getDateline("2017-05-02 19:31:47"));
//		System.out.println(getDateline("2017-05-11 16:13:49"));
		
//		
//		int f = 15 *24*60*60;
//		
//		//System.out.println(d1-f);
		
		//System.out.println( new Date(1320205608000l));
		//System.out.println( DateUtil.toString( new Date(1320205608000l),"yyyy-MM-dd HH:mm:ss"));
		
//		System.out.println(getDateline());
//		System.out.println(getDateline("2017-07-26 17:00:00", "yyyy-MM-dd HH:mm:ss"));
//		System.out.println(new Date().getTime());
//		long current=System.currentTimeMillis();//当前时间毫秒数
//		long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
//		long twelve=zero+24*60*60*1000-1;//今天23点59分59秒的毫秒数
//		long yesterday=System.currentTimeMillis()-24*60*60*1000;//昨天的这一时间的毫秒数
//		System.out.println(new Timestamp(current)+"--->"+current);//当前时间
//		System.out.println(new Timestamp(yesterday)+"--->"+yesterday);//昨天这一时间点
//		System.out.println(new Timestamp(zero)+"--->"+zero+"--->"+getDateStartLine());//今天零点零分零秒
//		System.out.println(new Timestamp(twelve)+"--->"+twelve+"--->"+getDateEndLine());//今天23点59分59秒
		
		System.out.println(getBetweenHourTime(getDateline(),getDateline()+5480));
		System.out.println(getTheCurrentMillionSeconds(new Date())/1000);
		
	}
}
